<script setup lang="ts">
import { http } from '@/services/http';
import { useUserStore } from '@/stores/user';
import type { login, ResultData } from '@/types';
import { TOKEN_KEY } from '@/types/constant';
import { ElMessage, type FormInstance } from 'element-plus';
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const userStore = useUserStore();
const ruleFormRef = ref<FormInstance>();
const dialogFindPwdVisible = ref(false);

const handleLogin = async (ruleFormRef: FormInstance | undefined) => {
  if (!ruleFormRef) {
    return;
  }
  await ruleFormRef.validate((valid, fields) => {
    if (valid) {
      http
        .post<ResultData>('/login', loginForm)
        .then((res) => {
          if (res.code === '200') {
            ElMessage({ type: 'success', message: res.msg });
            localStorage.setItem(TOKEN_KEY, res.data);
            userStore.token = res.data;
            userStore.updateUserInfo();
            router.push({ name: 'home' }); // 跳转到主页面
          } else {
            ElMessage({ type: 'error', message: res.msg || '登录失败' });
          }
        })
        .catch((error) => {
          ElMessage({ type: 'error', message: error });
        });
    } else {
      ElMessage({ type: 'error', message: '表单验证失败' });
    }
  });
};
const loginForm = reactive<login>({
  username: '',
  password: '',
});
const rule = reactive({
  username: [{ required: true, message: '请填写用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请填写密码', trigger: 'blur' }],
});

const handleFindPwd = () => {
  dialogFindPwdVisible.value = true;
};
</script>

<template>
  <div class="login-page">
    <img class="login-slogan" src="@/assets/kuai-kuai.png" alt="" />
    <div class="login-container">
      <div class="login-banner" />
      <div class="login-form">
        <div class="login-form-header">欢迎登录 KK used book</div>
        <el-form :model="loginForm" :rules="rule" ref="ruleFormRef">
          <el-form-item prop="username">
            <el-input prefix-icon="User" size="default" placeholder="请输入用户名" v-model="loginForm.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              prefix-icon="lock"
              size="default"
              placeholder="请输入密码"
              v-model="loginForm.password"
              type="password"
            />
          </el-form-item>
          <el-form-item>
            <el-button class="login-form-submit" color="blue" @click="handleLogin(ruleFormRef)">登 录</el-button>
          </el-form-item>
        </el-form>
        <div class="login-form-footer">
          <div class="reg">
            还没有账号？请
            <el-link type="primary" @click="$router.push({ name: 'register' })" :underline="false">注册</el-link>
          </div>
          <el-link @click="$router.push({ name: 'findPwd' })" :underline="false">忘记密码</el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.login-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #dadada;
}
.login-slogan {
  width: 600px;
  margin-top: -120px;
  margin-bottom: 40px;
}
.login-container {
  display: flex;
  width: 720px;
  height: 400px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 0 10px #ccc;
  z-index: 2;
  backdrop-filter: blur(20px);
  background: white;
}
.login-banner {
  width: 50%;
  background-image: url(@/assets/login.jpg);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.login-form {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  .el-form {
    width: 100%;
  }
}
.login-form-header {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 40px;
}
.login-form-submit {
  width: 100%;
}
.login-form-footer {
  display: flex;
  width: 100%;
  justify-content: space-between;
}
.reg {
  display: flex;
  align-items: center;
}
</style>
